/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainControlPanel.java
 *
 * Created on 2012/1/13, 上午 09:57:56
 */
package elaborate.tag_analysis.client.applications.flickr.ui;

import elaborate.tag_analysis.client.applications.ApplicationContext;
import elaborate.tag_analysis.client.applications.flickr.FlickrParams;
import elaborate.tag_analysis.client.utils.resource_logs.ResourceLogs;
import elaborate.task.Task;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lendle
 */
public class MainControlPanel extends javax.swing.JPanel implements FlickrParams {
    private ApplicationContext context=null;
    private ResourceLogs resourceLogs=null;

    //load conf props from saved file
    public void setAppProperties(Properties props){
        if(props!=null){
            this.txAppKey.setText(props.getProperty("appKey"));
            this.comboHotTagRange.setSelectedItem(props.getProperty("hotTagRange"));
            this.spinnerHotTagCount.setValue(Integer.valueOf(props.getProperty("hotTagCount")));
            this.spinnerMaxURLPerTag.setValue(Integer.valueOf(props.getProperty("maxURLPerTag")));
        }
    }
    //return conf as props
    public Properties getAppProperties(){
        Properties props=new Properties();
        props.setProperty("appKey", this.getApplicationKey());
        props.setProperty("hotTagRange", this.getHotTagsDuration());
        props.setProperty("hotTagCount", ""+this.getHotTagsCount());
        props.setProperty("maxURLPerTag", ""+this.getMaxURLPerTag());
        return props;
    }
    
    public ResourceLogs getResourceLogs() {
        return resourceLogs;
    }

    public void setResourceLogs(ResourceLogs resourceLogs) {
        this.resourceLogs = resourceLogs;
    }

    public ApplicationContext getContext() {
        return context;
    }

    public void setContext(ApplicationContext context) {
        this.context = context;
    }
    
    
    
    /** Creates new form MainControlPanel */
    public MainControlPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txAppKey = new javax.swing.JTextField();
        btReload = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        comboHotTagRange = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        spinnerHotTagCount = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        spinnerMaxURLPerTag = new javax.swing.JSpinner();

        jLabel1.setText("Flickr App Key:");

        txAppKey.setText("c7d511b53f5a75fe081a70dc0dc3f1fe");

        btReload.setText("Load/Reload");
        btReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btReloadActionPerformed(evt);
            }
        });

        jLabel2.setText("Hot Tag Range:");

        comboHotTagRange.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "week", "day" }));

        jLabel3.setText("Hot Tag Count:");

        spinnerHotTagCount.setModel(new javax.swing.SpinnerNumberModel(100, 0, 200, 1));

        jLabel4.setText("Max URL/Tag:");

        spinnerMaxURLPerTag.setModel(new javax.swing.SpinnerNumberModel(10, -1, 99999, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btReload, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(spinnerMaxURLPerTag, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                            .addComponent(spinnerHotTagCount, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                            .addComponent(comboHotTagRange, javax.swing.GroupLayout.Alignment.TRAILING, 0, 386, Short.MAX_VALUE)
                            .addComponent(txAppKey, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txAppKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboHotTagRange, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerHotTagCount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerMaxURLPerTag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(91, 91, 91)
                .addComponent(btReload)
                .addContainerGap(131, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btReloadActionPerformed
        try {
            // TODO add your handling code here:
            this.resourceLogs.clear();
            context.executeTask(new Task(){

                @Override
                public Object run() throws Exception {
                    context.reloadResources();
                    return null;
                }
            });
        } catch (Exception ex) {
            Logger.getLogger(MainControlPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btReloadActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btReload;
    private javax.swing.JComboBox comboHotTagRange;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSpinner spinnerHotTagCount;
    private javax.swing.JSpinner spinnerMaxURLPerTag;
    private javax.swing.JTextField txAppKey;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getApplicationKey() {
        if(this.txAppKey.getText()==null || this.txAppKey.getText().length()==0){
            return null;
        }
        return this.txAppKey.getText();
    }

    @Override
    public String getHotTagsDuration() {
        return (String) this.comboHotTagRange.getSelectedItem();
    }

    @Override
    public int getHotTagsCount() {
        return Integer.valueOf(""+this.spinnerHotTagCount.getValue());
    }

    @Override
    public int getMaxURLPerTag() {
        return Integer.valueOf(""+this.spinnerMaxURLPerTag.getValue());
    }
}
